package cn.tedu.ivos.user.service.impl;

import cn.tedu.ivos.base.exception.ServiceException;
import cn.tedu.ivos.base.response.StatusCode;
import cn.tedu.ivos.user.mapper.UserMapper;
import cn.tedu.ivos.user.pojo.dto.UserLoginParam;
import cn.tedu.ivos.user.pojo.vo.UserVO;
import cn.tedu.ivos.user.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 业务层实现类需要添加@Service注解，当Autowire时可以将该实现类作为依赖注入给接口[多态]
 */
@Slf4j
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public UserVO login(UserLoginParam userLoginParam) {
        log.debug("登录业务：" + userLoginParam);
        String username = userLoginParam.getUsername();
        UserVO userVO = userMapper.selectByUsername(username);
        String passwordVO = userVO.getPassword();
        String password = userLoginParam.getPassword();
        if(userVO == null){
            throw new ServiceException(StatusCode.USERNAME_ERROR);
        }
        if(!passwordVO.equals(password)){
            throw new ServiceException(StatusCode.PASSWORD_ERROR);
        }
        return userVO;
    }
}
